package design.simdi.code.final_paper.site.panels.licente.proiecte;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Criterion;

import design.simdi.code.final_paper.core.daos.ProjectDao;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.Project;
import design.simdi.code.final_paper.core.entities.Sesiune;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 12:18:26 AM)
 * 
 */
public class SortableProjectDataProvider extends SortableDataProvider<Project> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private ProjectDao projectDao;
	private Person owner;
	private Sesiune sesiune;
	private List<Criterion> criterii;

	/**
	 * constructor
	 */
	public SortableProjectDataProvider() {
		InjectorHolder.getInjector().inject(this);
		// set default sort
		setSort("name", true);
		criterii = new ArrayList<Criterion>();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int,
	 *      int)
	 */
	public Iterator<Project> iterator(int first, int count) {
		SortParam sp = getSort();
		// Order o;
		// if (sp.isAscending())
		// o = Order.asc(sp.getProperty());
		// else
		// o = Order.desc(sp.getProperty());
		String sortQuery;
		sortQuery = " order by ";

		if (sp.getProperty().equals("name"))
			sortQuery += "project." + sp.getProperty();
		else if (sp.getProperty().equals("author")) {
			if (sp.isAscending())
				sortQuery += "author.prenume asc" + ",  " + "author.nume";
			else
				sortQuery += "author.prenume desc" + ",  " + "author.nume";
		} else
			sortQuery += sp.getProperty();
		// Order o;
		if (sp.isAscending())
			sortQuery += " asc";
		else
			sortQuery += " desc";
		//
		String query = "";

		// return getProjectDao().findAll(new Order[] { o },
		// criterii).subList(first, first + count).iterator();
		if (owner != null)
			query = String
					.format(
							"select distinct project from Project as project, Person as author, Sesiune as sesiune where "
									+ "project.author = author  and author.id = '%s' and project.sesiune = sesiune and sesiune.id= '%s'  "
									+ sortQuery, getOwner().getId(), getSesiune().getId());
		else
			query = String
					.format(
							"select distinct project from Project as project, Person as author, Sesiune as sesiune where "
									+ "project.author = author and project.sesiune = sesiune and sesiune.id= '%s' "
									+ sortQuery, getSesiune().getId());
		List<Project> lista = projectDao.find(query);

		return lista.subList(first, first + count).iterator();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		// return getProjectDao().findAll(new Order[] {}, criterii).size();
		SortParam sp = getSort();
		String sortQuery;
		sortQuery = " order by ";

		if (sp.getProperty().equals("name"))
			sortQuery += "project." + sp.getProperty();
		else
			sortQuery += sp.getProperty();
		// Order o;
		if (sp.isAscending())
			sortQuery += " asc";
		else
			sortQuery += " desc";
		//
		String query = "";
		if (owner != null)
			query = String
					.format(
							"select distinct project from Project as project, Person as author, Sesiune as sesiune where "
									+ "project.author = author  and author.id = '%s' and project.sesiune = sesiune and sesiune.id= '%s'  "
									+ sortQuery, getOwner().getId(), getSesiune().getId());
		else
			query = String.format(
					"select distinct project from Project as project, Person as author, Sesiune as sesiune where "
							+ "project.author = author and  project.sesiune = sesiune and sesiune.id= '%s' "
							+ sortQuery, getSesiune().getId());
		List<Project> lista = projectDao.find(query);

		return lista.size();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<Project> model(Project object) {
		return new DetachableProjectModel(object, getProjectDao());
	}

	public void setCriterii(List<Criterion> criterii) {
		this.criterii = criterii;
	}

	public List<Criterion> getCriterii() {
		return criterii;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setOwner(Person owner) {
		this.owner = owner;
	}

	public Person getOwner() {
		return owner;
	}

	public void setSesiune(Sesiune sesiune) {
		this.sesiune = sesiune;
	}

	public Sesiune getSesiune() {
		return sesiune;
	}

}